﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JYStockAnalyser.Model;
using JYStockAnalyser.DA;

namespace JYStockAnalyser.Face
{
    public class EstimateRuleManageFace : System.MarshalByRefObject
    {
        public byte CreateNewRule(YearQuarter beginQuarter, YearQuarter endQuarter)
        {
            EstimateRule rule = new EstimateRule();

            rule.BeginYearQuarter.Year = beginQuarter.Year;
            rule.BeginYearQuarter.Quarter = beginQuarter.Quarter;
            rule.EndYearQuarter.Year = endQuarter.Year;
            rule.EndYearQuarter.Quarter = endQuarter.Quarter;

            rule.ASafetyCoefficient = 0.7m;
            rule.BSafetyCoefficient = 0.9m;
            rule.CSafetyCoefficient = 1.1m;
            rule.DSafetyCoefficient = 1.3m;
            rule.ESafetyCoefficient = 1.5m;

            rule.CommonCoefficients.Add("溢价", 1m);
            rule.CommonCoefficients.Add("行情", 1m);

            List<string> trades = new StockManageFace().GetAllTrades();

            foreach (string trade in trades)
            {
                rule.TradeCoefficients.Add(trade, 1m);
            }

            EstimateRuleMapper mapper = new EstimateRuleMapper();
            try
            {
                mapper.InsertEstimateRule(rule);
                return 1;
            }
            catch (ORException orErr)
            {
                if (orErr.ErrorNumber == ORException.ERROR_NAME_EXISTS)
                {
                    return 7;
                }

                if (orErr.ErrorNumber == ORException.ERROR_OTHER)
                {
                    return 110;
                }
                return 110;
            }
        }

        public byte DeleteRule(int ruleID)
        {
            EstimateRuleMapper mapper = new EstimateRuleMapper();
            try
            {
                mapper.DeleteEstimateRule(ruleID);
                return 1;
            }
            catch (ORException orErr)
            {
                if (orErr.ErrorNumber == ORException.ERROR_OTHER)
                {
                    return 110;
                }
                return 110;
            }
        }

        public byte UpdateRule(EstimateRule rule)
        {
            EstimateRuleMapper mapper = new EstimateRuleMapper();
            try
            {
                mapper.UpdateEstimateRule(rule);
                return 1;
            }
            catch (ORException orErr)
            {
                if (orErr.ErrorNumber == ORException.ERROR_NAME_EXISTS)
                {
                    return 7;
                }
                if (orErr.ErrorNumber == ORException.ERROR_OTHER)
                {
                    return 110;
                }
                return 110;
            }
        }

        public Dictionary<KeyValuePair<YearQuarter, YearQuarter>, int> FindAllRuleYearQuarterAndIds()
        {
            EstimateRuleMapper mapper = new EstimateRuleMapper();
            return mapper.FindAllRuleYearQuarterAndIds();
        }

        public EstimateRule GetRuleById(int id)
        {
            EstimateRuleMapper mapper = new EstimateRuleMapper();
            return mapper.FindRuleById(id);
        }
    }
}
